package com.wdciti.sccp.iot.springvertx.protocols.mqtt;

import com.wdciti.sccp.iot.common.endpoint.EndpointConfig;
import com.wdciti.sccp.iot.common.endpoint.EndpointType;
import io.vertx.core.json.JsonObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author early
 */
@EqualsAndHashCode
@Data
@Configuration
public class MqttClientEndpointConfig implements EndpointConfig {

    private String name;

    private EndpointType endpointType;

//    private String codec;


    private boolean enable = false;

    private Integer instance = 1;

    private Boolean isHa = false;

    private Boolean isWorker = false;

    private Map<String, Object> configJson = new HashMap<>();

    private Integer port;

    private String host;

    private String clientId = UUID.randomUUID().toString();

    private String username;

    private String password;

    private List<Topic> topics = new ArrayList<>();

    public MqttClientEndpointConfig() {
        this.endpointType = EndpointType.MQTT_CLIENT;
    }

    @Override
    public Map<String, Object> getConfigMap() {
        configJson.put("host", host);
        configJson.put("port", port);

        JsonObject option = JsonObject.of();
        option.put("clientId", clientId);
        if (StringUtils.hasText(username)) {
            option.put("username", username);
        }
        if (StringUtils.hasText(password)) {
            option.put("password", password);
        }
        configJson.put("options", option);
        if (!topics.isEmpty()) {
            configJson.put("topics", topics);
        }

        configJson.put("instance", instance);
        configJson.put("isHa", isHa);
        configJson.put("isWorker", false);
        return configJson;
    }


    @Data
    protected static class Topic {
        String name;
        int qos;
    }
}
